#include "QProjectTreeWidget.h"
#include <QStandardItemModel>
#include <QStyleFactory>

QProjectTreeWidget::QProjectTreeWidget(QWidget* parent)
    : QTreeWidget(parent)
{
    setContextMenuPolicy(Qt::CustomContextMenu);
    reset();
    setHeaderHidden(true);
    setStyle(QStyleFactory::create("windows"));
    setIconSize(QSize(24, 24));
    m_modeData = ModelXml::GetInstance();
}

void QProjectTreeWidget::AddProject(stProject project, bool writexml)
{
    clear();

    m_projectItem = new QTreeWidgetItem(this);
    QIcon icon1;
    icon1.addPixmap(QPixmap(":/resource/Project.png"), QIcon::Normal);
    m_projectItem->setData(0, Qt::UserRole, Node_Project);
    m_projectItem->setIcon(0, icon1);
    m_projectItem->setText(0, project.name.c_str());

    m_collectItem = new QTreeWidgetItem(m_projectItem);
    QIcon icon2;
    icon2.addPixmap(QPixmap(":/resource/CollectChannel.png"), QIcon::Normal);
    m_collectItem->setIcon(0, icon2);
    m_collectItem->setData(0, Qt::UserRole, Node_CollectChannel);
    m_collectItem->setText(0, "采集通道");
    m_collectItem->setTextColor(0, "#1296db");
    QFont font = m_collectItem->font(0);
    //font.setBold(false);
    font.setFamily("微软雅黑");
    font.setBold(true);
    //font.setAntialiasing(true);
    m_collectItem->setFont(0, font);

    m_updateItem = new QTreeWidgetItem(m_projectItem);
    QIcon icon3;
    icon3.addPixmap(QPixmap(":/resource/updateChannel.png"), QIcon::Normal);
    m_updateItem->setIcon(0, icon3);
    m_updateItem->setData(0, Qt::UserRole, Node_UpdateChannel);
    m_updateItem->setText(0, "转发通道");
    m_updateItem->setTextColor(0, "#1296db");
    m_updateItem->setFont(0, font);
    expandAll();
    if (writexml)
        m_modeData->AddProject(project);
}

void QProjectTreeWidget::AddCollectChannel(stCollectChannel channel, bool writexml)
{
    QTreeWidgetItem* channelItem = new QTreeWidgetItem(m_collectItem);
    QIcon icon1;
    icon1.addPixmap(QPixmap(":/resource/UserCollectChannel.png"), QIcon::Normal);
    channelItem->setData(0, Qt::UserRole, Node_CollectChannelArray);
    channelItem->setData(1, Qt::UserRole, channel.id);
    channelItem->setIcon(0, icon1);
    channelItem->setText(0, QString(channel.name.c_str()) + ":" + QString(channel.protoName.c_str()).toUpper());
    m_collectChannelItem.insert(channel.id, channelItem);
    if (writexml)
        m_modeData->AddCollectChannel(channel);
}

void QProjectTreeWidget::AddCollectDevice(int channelID, stCollecDevice device, bool writexml)
{
    QTreeWidgetItem* channelItem = m_collectChannelItem.value(channelID);
    if (!channelItem)
        return;
    QTreeWidgetItem* devItem = new QTreeWidgetItem(channelItem);
    QIcon icon1;
    stCollectChannel channel = m_modeData->GetCollectChannel(channelID);
    if (channel.portType == stCollectChannel::ChannelPortType::Port_SerialPort) {
        icon1.addPixmap(QPixmap(":/resource/comm.png"), QIcon::Normal);
    } else {
        icon1.addPixmap(QPixmap(":/resource/network.png"), QIcon::Normal);
    }

    devItem->setData(0, Qt::UserRole, Node_CollectDev);
    devItem->setData(1, Qt::UserRole, device.id);
    devItem->setIcon(0, icon1);
    devItem->setText(0, QString("设备名称:") + device.name.c_str());

    if (writexml)
        m_modeData->AddCollectDevice(channelID, device);
    expandItem(channelItem);
}

void QProjectTreeWidget::DelCollectChannel(int channelID)
{
    currentItem()->parent()->removeChild(currentItem());
    m_modeData->DelCollectChannel(channelID);
}

void QProjectTreeWidget::DelCollectDevice(int channelID, int deviceID)
{
    currentItem()->parent()->removeChild(currentItem());
    m_modeData->DelCollectDevice(channelID, deviceID);
}

void QProjectTreeWidget::DelUpdateChannel(int channelID)
{
    currentItem()->parent()->removeChild(currentItem());
    m_modeData->DelUpdateChannel(channelID);
}

int QProjectTreeWidget::GetCollectChannelMaxID()
{
    return m_modeData->GetMaxCollectChannelID();
}

//int QProjectTreeWidget::GetCollectDeviceMaxID()
//{
//    return m_modeData->GetDeviceNumberMax();
//}

void QProjectTreeWidget::AddUpdateChannel(stUpdateChannel updateChannel, bool writexml)
{
    QTreeWidgetItem* item = new QTreeWidgetItem(m_updateItem);
    QIcon icon;
    icon.addPixmap(QPixmap(":/resource/UserUpdateChannel.png"), QIcon::Normal);
    item->setIcon(0, icon);
    item->setText(0, QString("通道名称:") + updateChannel.name.c_str());
    item->setData(0, Qt::UserRole, Node_UpdateChannelDEV);
    item->setData(1, Qt::UserRole, updateChannel.id);
    if (writexml)
        m_modeData->AddUpdateChannel(updateChannel);
}

//打开工程
bool QProjectTreeWidget::InitOpenProject(QString filePath)
{
    //更改路径
    QString pathDir = filePath.left(filePath.lastIndexOf("/"));
    if (m_modeData->OpenProjectFile(filePath.toStdString())) {
        m_modeData->GetProject().path = pathDir.toStdString();
        AddProject(m_modeData->GetProject(), false);
        std::map<int, stCollectChannel> channels = m_modeData->GetCollectChannel();
        for (std::map<int, stCollectChannel>::iterator it = channels.begin(); it != channels.end(); it++) {
            AddCollectChannel(it->second, false);
            std::map<int, stCollecDevice> devices = m_modeData->GetCollectDevice(it->second.id);
            for (std::map<int, stCollecDevice>::iterator itDevice = devices.begin(); itDevice != devices.end(); itDevice++) {
                AddCollectDevice(it->second.id, itDevice->second, false);
            }
        }

        std::map<int, stUpdateChannel> updateChannels = m_modeData->GetUpdateChannelList();
        for (std::map<int, stUpdateChannel>::iterator it = updateChannels.begin(); it != updateChannels.end(); it++) {
            AddUpdateChannel(it->second, false);
        }

        return true;
    }
    return false;
}

bool QProjectTreeWidget::DeleteProject()
{
    this->clear();
    reset();
    //    if (m_projectItem)
    //        delete m_projectItem;
    //    if (m_collectItem)
    //        delete m_collectItem;
    //    if (m_updateItem)
    //        delete m_updateItem;

    return m_modeData->DeleteProject();
}
